<?php
//初始化
require(dirname(__FILE__) . '/includes/init1.php');
require(dirname(__FILE__) . '/includes/simple_html_dom.php');
set_time_limit(0);
$group_list = $db->getAll("select group_id,link from ".$ecs->table('jd_group')." where end_time=0");
foreach($group_list as $index=>$group){
	$group_id = $group['group_id'];
	$link = $group['link'];
	//获取产品id
	$link = "http://tuan.360buy.com/team-10239712.html";
	$id = get_str($link,'http://tuan.360buy.com/team-','.html');
	$curl = curl_init(); 
	curl_setopt($curl, CURLOPT_URL,$link); 
	curl_setopt($curl, CURLOPT_REFERER, 'http://www.360buy.com'); 
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
	$result = curl_exec($curl); 
	curl_close($curl);
	//echo $result;
	$html = str_get_html($result);
	$num = 0;
	foreach($html->find('#deal-status-'.$id) as $h){
		$num = get_str($h->innertext,'<span><strong>','</strong> 人已购买');
	}
	foreach($html->find('#counter'.$id) as $e){
		$is_end = $e->innertext;
	}
	if(!$is_end){
		$db->query("update ".$ecs->table('jd_group')."set end_time='".time()."' where group_id='".$group_id."'");
	}
	$sql = "insert into ".$ecs->table('jd_buy')." (group_id,buys_num,add_time) values('".$group_id."','".$num."','".time()."')";
	$db->query($sql);
}

function get_str($str,$start_str,$end_str){
   $start_pos = strpos($str,$start_str)+strlen($start_str);  
   $end_pos = strpos($str,$end_str);  
   $c_str_l = $end_pos - $start_pos;  
   $contents = substr($str,$start_pos,$c_str_l);  
   return $contents;  
} 
?>
